/*
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#include <core/interrupt.h>
#include <core/schedule.h>
#include <caps/capability.h>
#include <caps/cnode.h>
#include <caps/irq.h>
#include <uapi/errors.h>
#include <uapi/ipcops.h>

void cap_irqc_init(capability_t *cap)
{
    cap_basic_init(cap, CAP_IRQ_CONTROL);
}

static inline void cap_irqh_set_obj(capability_t *cap, notification_t *ntfn)
{
    cap_set_long_info(cap, (unsigned long)ntfn);
}

static inline notification_t *cap_irqh_get_obj(capability_t *cap)
{
    return (notification_t *)cap_get_long_info(cap);
}

void cap_irqh_init(capability_t *cap, notification_t *ntfn)
{
    cap_basic_init(cap, CAP_IRQ_HANDLER);
    cap_irqh_set_obj(cap, ntfn);
}

void cap_irqc_dispatcher(capability_t *cap, unsigned long tag, unsigned long cptr, unsigned long irqn, unsigned long m0,
                         unsigned long m1, unsigned long m2, unsigned long m3)
{
    (void)cap;
    (void)m0;
    (void)m1;
    (void)m2;
    (void)m3;
    capability_t   *to;
    notification_t *ntfn;
    long            retcode;

    to = cnode_cptr_lookup(task_get_cnode(current()), cptr, sizeof(unsigned long) * 8U);
    if (to == NULL) {
        task_set_retcode(current(), -EINVALID);
        return;
    }

    switch (msgtag_get_op(tag)) {
        case CAP_IRQ_OP_GET:
            if (cap_is_empty(to) != 0) {
                ntfn = irq_register(irqn);
                if (ntfn != NULL) {
                    cap_irqh_init(to, ntfn);
                    retcode = ESUCCESS;
                } else {
                    retcode = -EINVALID;
                }
            } else {
                retcode = -EOVERLAP;
            }
            break;

        case CAP_IRQ_OP_PUT:
            if (cap_get_type(to) == CAP_IRQ_HANDLER) {
                ntfn = cap_irqh_get_obj(to);
                irq_unregister(ntfn);
                cap_make_empty(to);
                retcode = ESUCCESS;
            } else {
                retcode = -EINVALID;
            }
            break;

        default:
            retcode = -ENOSYS;
            break;
    }

    task_set_retcode(current(), retcode);
}

void cap_irqh_dispatcher(capability_t *cap, unsigned long tag, unsigned long m0, unsigned long m1, unsigned long m2,
                         unsigned long m3, unsigned long m4, unsigned long m5)
{
    (void)m0;
    (void)m1;
    (void)m2;
    (void)m3;
    (void)m4;
    (void)m5;
    notification_t *ntfn = (void *)cap_irqh_get_obj(cap);

    switch (msgtag_get_op(tag)) {
        case CAP_IRQ_OP_WAIT:
            irq_prepare_wait(ntfn);
            notification_wait(ntfn, current(), 1);
            break;

        case CAP_IRQ_OP_ACK:
            irq_ack_enable(ntfn);
            task_set_retcode(current(), ESUCCESS);
            break;

        case CAP_IRQ_OP_ACK_WAIT:
            irq_ack_enable(ntfn);
            notification_wait(ntfn, current(), 1);
            break;

        case CAP_IRQ_OP_PUT:
            irq_unregister(ntfn);
            cap_make_empty(cap);
            task_set_retcode(current(), ESUCCESS);
            break;

        default:
            task_set_retcode(current(), ERR_TO_RETCODE(-ENOSYS));
            break;
    }
}
